﻿//LoadPage
var loadPage = function (tabPanel, record) {
    var tab = tabPanel.getComponent(record.getId());
    if (!tab) {
        tab = tabPanel.add({
            id: record.getId(),
            title: record.data.text,
            closable: true,
            loader: {
                url: record.data.href,
                renderer: "frame",
                loadMask: {
                    showMask: true,
                    msg: record.data.href
                }
            },
            autoScroll: true
        });
    }

    tabPanel.setActiveTab(tab);
};
//AddTabs
var addTab = function (tabPanel, record) {
    var text = record.data.text,
        tab = tabPanel.getComponent(text);

    if (!tab) {
        tab = tabPanel.add({
            id: text,
            title: text,
            closable: true,
            loader: {
                url: record.data.url,
                renderer: "frame",
                loadMask: {
                    showMask: true,
                    msg: "Đang tải " + text + "..."
                }
            }
        });
    }
    tabPanel.setActiveTab(tab);
};
//FormatNumber-GetNumber
var FormatNumber = function (str) {
    var strTemp = GetNumber(str);
    if (strTemp.length <= 3)
        return strTemp;
    var strResult = "";
    var i;
    for (i = 0; i < strTemp.length; i++)
        strTemp = strTemp.replace(/[,]/g, "");
    for (i = strTemp.length; i >= 0; i--) {
        if (strResult.length > 0 && (strTemp.length - i - 1) % 3 == 0)
            strResult = "," + strResult;
        strResult = strTemp.substring(i, i + 1) + strResult;
    }
    return strResult;
};
function GetNumber(str) {
    for (var i = 0; i < str.length; i++) {
        var temp = str.substring(i, i + 1);
        if (!(temp == "," || temp == "." || (temp >= 0 && temp <= 9))) {
            alert("Vui lòng nhập số (0-9)!");
            return str.substring(0, i);
        }
        if (temp == " ")
            return str.substring(0, i);
    }
    return str;
};
//Grid-Null
var gridNull = function (value) {
    if (value == "") {
        return "-";
    } else {
        return value;
    }
};
var submitValue = function (grid, hiddenFormat, format) {
    hiddenFormat.setValue(format);
    grid.submitData(false, { isUpload: true });
};
var template = '<span style="color:{0};">{1}</span>';
var change = function (value) {
    return window.Ext.String.format(template, (value > 0) ? "green" : "red", value);
};
var pctChange = function (value) {
    return window.Ext.String.format(template, (value > 0) ? "green" : "red", value + "%");
};
var indMoney = function (v) {
    v = (Math.round((v - 0) * 100)) / 100;
    v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
    v = String(v);
    var ps = v.split('.'),
        whole = ps[0],
        sub = ps[1] ? '.' + ps[1] : '.00',
        r = /(\d+)(\d{3})/;
    while (r.test(whole)) {
        whole = whole.replace(r, '$1' + ',' + '$2');
    }
    v = whole;

    return v;
};
var NumCheck = function () {
    var x = window.App.txtSumPrice.getValue();
    var y = window.App.txtSumVAT.getValue();
    var k = window.App.txtShippingFee.getValue();
    var z = parseFloat(x.replace(/[,]/g, "")) + parseFloat(y.replace(/[,]/g, "")) + parseFloat(k.replace(/[,]/g, ""));
    window.App.txtSumTotalCash.setValue(z);
    window.App.txtShippingFee.setValue(FormatNumber(k));
};
//Disable-Keyboard-Backspace
$(document).keydown(function (e) {
    var doPrevent;
    if (e.keyCode == 8) {
        var d = e.srcElement || e.target;
        if (d.tagName.toUpperCase() == 'INPUT' || d.tagName.toUpperCase() == 'TEXTAREA') {
            doPrevent = d.readOnly || d.disabled;
        }
        else
            doPrevent = true;
    }
    else
        doPrevent = false;

    if (doPrevent)
        e.preventDefault();
});